package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pojo.SysRight;
import com.ktjy.pojo.SysRole;
import com.ktjy.service.ISysRightService;
import com.ktjy.service.ISysRoleService;
import jakarta.annotation.Resource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hjy
 * @since 2025-01-09
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {
    @Resource
    ISysRoleService sysRoleService;
    @Resource
    ISysRightService sysRightService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Object findAll() {
        return sysRoleService.list();
    }

    @RequestMapping("/list")
    public String list(SysRole sysRole,
                       @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                       Model model) {
        Page page=new Page(pageIndex,pageSize);
        QueryWrapper qw=new QueryWrapper<>();
        if (sysRole.getRoleName()!=null){
            qw.like("role_name",sysRole.getRoleName());
        }
        IPage<SysRole> rolePage=sysRoleService.page(page,qw);
        model.addAttribute("rolePage",rolePage);
        model.addAttribute("sysRole",sysRole);
        return "role/list";
    }

    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("sysRole",new SysRole());
        List<SysRight> rights=sysRightService.list();
        model.addAttribute("rights",rights);
        return "role/add";
    }

    @PostMapping("/save")
    public String save(SysRole sysRole) {
        boolean ok=false;
        if (sysRole.getRoleId()==null){
            ok=sysRoleService.save(sysRole);
        }else {
            ok=sysRoleService.updateById(sysRole);
        }
        if (ok){
            return "redirect:/role/list";
        }
        return "role/add";
    }

    @GetMapping("/edit")
    public String edit(Model model,@RequestParam("roleId") Integer roleId) {
        SysRole role=sysRoleService.getById(roleId);
        model.addAttribute("role",role);
        List<SysRight> rights=sysRightService.list();
        model.addAttribute("rights",rights);
        // 获取该角色已拥有的权限集合，假设SysRole有对应的获取方法
//        List<String> roleRights = role.get();
//        model.addAttribute("roleRights", roleRights);
        return "role/edit";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Integer id) {
        Map<String,Object> map=new HashMap<>();
        boolean ok=sysRoleService.removeById(id);
        if (ok){
            map.put("delResult",true);
        }else {
            map.put("delResult",false);
        }
        return map;
    }
}
